package com.app.twth.ui.adjust.retallytask;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.databinding.ItemTallyLpnListBinding;
import com.app.twth.interfaces.MyOnClickListener;
import com.app.twth.ui.tally.lpn.TalliedSku;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;


public class ReTallyLPNAdapter extends RecyclerView.Adapter<ReTallyLPNAdapter.MyViewHolder> {
    private LayoutInflater mInflater;
    private List<TalliedSku> data = new ArrayList<>();
    private MyOnClickListener locationListener;

    public ReTallyLPNAdapter(Context mContext, List<TalliedSku> data, MyOnClickListener locationListener) {
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.locationListener = locationListener;
    }

    public void setData(List<TalliedSku> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemTallyLpnListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemTallyLpnListBinding itemBinding;

        public MyViewHolder(ItemTallyLpnListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            TalliedSku tallyLPN = data.get(position);
            itemBinding.tvItem.setText(tallyLPN.getIbLpn() + "");
            itemBinding.tvInventory.setText(tallyLPN.getQty() + "");
            if ((!Utils.isEmpty(tallyLPN.getStatus())) && (tallyLPN.getStatus().equals("2"))) {
                itemBinding.ivGo.setVisibility(View.GONE);
            } else {
                itemBinding.ivGo.setVisibility(View.VISIBLE);
            }
            itemBinding.layout.setOnClickListener(v -> {
                if (locationListener == null) {
                    return;
                }
                locationListener.OnClick(position);
            });
        }
    }
}
